package com.dtagri.fjwlw.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.adapter.VideoCenterAdapter;
import com.dtagri.fjwlw.api.FJwlwApi;
import com.dtagri.fjwlw.base.BaseSimpleFragment;
import com.dtagri.fjwlw.base.TGson;
import com.dtagri.fjwlw.bean.TjCompanyRegion;
import com.dtagri.fjwlw.bean.TjNvr;
import com.dtagri.fjwlw.bean.TjNvrChanel;
import com.dtagri.fjwlw.bean.tdvideo.VideoInfo;
import com.dtagri.fjwlw.broadcastreceiver.TReceiver;
import com.dtagri.fjwlw.ui.VView;
import com.dtagri.fjwlw.ui.VideoCenterActivity;
import com.dtagri.fjwlw.util.HKVideoHelper;
import com.dtagri.fjwlw.util.TDVideoHelper;
import com.dtagri.fjwlw.util.TToast;
import com.dtagri.fjwlw.widget.EmptyLayout;
import com.google.gson.reflect.TypeToken;


/**
 * 视频中心
 * 
 * @author wxjn
 * 
 */
public class VideoCenterFragment extends BaseSimpleFragment implements
OnTouchListener {
	protected ListView listView;

	protected List<TjNvr> tjNvrs = new ArrayList<TjNvr>();

	private VideoCenterAdapter videoCenterAdapter;

	private SurfaceView m_osurfaceView = null;

	HKVideoHelper hKVideoHelper;
	private RelativeLayout bottomView;

	private ImageButton videoRight;

	private ImageButton videoLeft;

	private TReceiver tReceiver;
	private RelativeLayout relativeLayout;
	private TDVideoHelper helper;
	private VView vview;
	private RelativeLayout m_LayoutBody;
	private ScrollView scrollView;
	private long mLastTime=0,mCurTime;
	public static int state;//0竖屏 1横屏
	private VideoCenterActivity videoCenterActivity;
	private SurfaceView m_osurfaceView2=null;
	private ImageView video_img;
	
	@Override
	protected void xOnSuccess(byte[] arg2) {
		Type type = new TypeToken<List<TjNvr>>() {
		}.getType();
		tjNvrs = TGson.gson.fromJson(new String(arg2), type);

		if (tjNvrs.size() == 0) {
			baseSimpleActivity.getmErrorLayout().setErrorType(
					EmptyLayout.NODATA);
			baseSimpleActivity.getmErrorLayout().setErrorMessage("该基地暂无NVR");
		}

		videoCenterAdapter = new VideoCenterAdapter(getActivity(), this, tjNvrs);
		listView.setAdapter(videoCenterAdapter);
	}

	@Override
	public void initView(View view) {
		super.initView(view);
		state=0;
		tReceiver = new TReceiver(getActivity(), TReceiver.CONTROL_BACK) {

			@Override
			public void onReceive(Context arg0, Intent arg1) {
				hKVideoHelper.stopSinglePlayer();
				hKVideoHelper.Cleanup();
			}
		};
		videoCenterActivity=new VideoCenterActivity();
		listView = (ListView) view.findViewById(R.id.listview);

		m_osurfaceView = (SurfaceView) view.findViewById(R.id.Sur_Player);
		bottomView = (RelativeLayout) view.findViewById(R.id.rl_bottom_view);
		videoLeft = (ImageButton) view.findViewById(R.id.iv_video_left);
		videoRight = (ImageButton) view.findViewById(R.id.iv_video_right);
		videoLeft.setOnTouchListener(this);
		videoRight.setOnTouchListener(this);
		hKVideoHelper = new HKVideoHelper(getActivity(), m_osurfaceView);
		relativeLayout=(RelativeLayout) view.findViewById(R.id.rl_bottom_view2);

		scrollView=(ScrollView) view.findViewById(R.id.ScrollView);
		video_img=(ImageView) view.findViewById(R.id.video_img);
		//获取屏幕大小
		DisplayMetrics dm=new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width=dm.widthPixels;
		int height=dm.heightPixels;
		m_LayoutBody=(RelativeLayout) view.findViewById(R.id.rl_bottom_view);
		vview=(VView) view.findViewById(R.id.Real_Time_view_video1);
		helper=new TDVideoHelper(getActivity(),vview,width,height,m_LayoutBody);
		
		//天地伟业视频点击监听
		vview.setOnClickListener(new OnClickListener() {
			//双击放大视频和缩小事件
			@Override
			public void onClick(View arg0) {

				mCurTime=System.currentTimeMillis();
				if(mCurTime-mLastTime>800){
					//					Toast.makeText(getActivity(), "再按一次切换大小", 500).show();
					mLastTime=mCurTime;
				}else{
					if(state==0){
						//						Toast.makeText(getActivity(), "从竖屏切换为横屏 ", 500).show();
						scrollView.setVisibility(View.GONE);
						Message msg1=new Message();
						msg1.arg1=77;

						VideoCenterActivity.mymsgHandler.sendMessage(msg1);
						state=1;
						Message msg=new Message();
						msg.arg1=88;
						helper.msgHandler.sendMessage(msg);

					}else{
						//						Toast.makeText(getActivity(), "从横屏切换为竖屏 ", 500).show();
						state=0;
						scrollView.setVisibility(View.VISIBLE);
						Message msg1=new Message();
						msg1.arg1=78;

						VideoCenterActivity.mymsgHandler.sendMessage(msg1);

						//通知帮助类重新适应屏幕大小
						Message msg=new Message();
						msg.arg1=88;
						helper.msgHandler.sendMessage(msg);
					}

				}

			}
		});
		
		//海康视频点击监听
		m_osurfaceView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mCurTime=System.currentTimeMillis();
				if(mCurTime-mLastTime>800){
					mLastTime=mCurTime;
				}else{
					if(state==0){
						scrollView.setVisibility(View.GONE);
						Message msg1=new Message();
						msg1.arg1=77;

						VideoCenterActivity.mymsgHandler.sendMessage(msg1);
						state=1;
					}
					else{
						state=0;
						scrollView.setVisibility(View.VISIBLE);
						Message msg1=new Message();
						msg1.arg1=78;

						VideoCenterActivity.mymsgHandler.sendMessage(msg1);
					}
					
				}
				
			}
		});
	}

	@Override
	protected void xInitData() {
		TjCompanyRegion tjCompanyRegion = AppContext.user.getTjCompanyRegions()
				.get(AppContext.userOperate.getCurBaseIndex());
		FJwlwApi.videoList(AppContext.user.getUsername(), mHandler);
	}

	@Override
	protected int getLayoutId() {
		return R.layout.layout_video_center;
	}

	@Override
	protected String getCacheKey() {
		return null;
	}
	/**
	 * 
	 * 切换至海康摄像头
	 * @param tjNvrChanel
	 * @param nvrEntity
	 */
	public void changeCamera(TjNvrChanel tjNvrChanel, TjNvr nvrEntity) {

		if(helper.m_cls_VideoViewController.HasPlayed()){
			helper.m_cls_VideoViewController.StopVideo();
		}
		video_img.setVisibility(View.GONE);
		hKVideoHelper.reLogin(nvrEntity);

		if (tjNvrChanel.getType() == 0) {// 球机
			videoLeft.setVisibility(View.VISIBLE);
			videoRight.setVisibility(View.VISIBLE);
		} else {
			videoLeft.setVisibility(View.GONE);
			videoRight.setVisibility(View.GONE);
		}
		bottomView.setVisibility(View.VISIBLE);
		hKVideoHelper.preview(tjNvrChanel.getSschanelnum());
	}
	/**
	 * 
	 * 切换至天地伟业摄像头
	 * @param tjNvr
	 * @param tjNvrChanel
	 */
	public void change(TjNvr tjNvr,TjNvrChanel tjNvrChanel){
		//关闭原先开启的摄像头
		if(helper.m_cls_VideoViewController.HasPlayed()){
			helper.m_cls_VideoViewController.StopVideo();
		}
		hKVideoHelper.Cleanup();
		video_img.setVisibility(View.GONE);
		helper.bindingVideoParam(tjNvr,tjNvrChanel);

		bottomView.setVisibility(View.GONE);

		relativeLayout.setVisibility(View.VISIBLE);

	}
	
	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		int id = arg0.getId();
		switch (id) {
		case R.id.iv_video_left:
			hKVideoHelper.ptz(arg1, false);
			break;
		case R.id.iv_video_right:
			hKVideoHelper.ptz(arg1, true);
			break;
		}
		return false;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		tReceiver.unregisterReceiver();
	}


}
